﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ShopScreen.cs" company="">
//   
// </copyright>
// <summary>
// A screen where the player buy stuff.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

/*
 * Author: Juan Mompean Esteban
 * Created: 30-11-2011
 */
namespace ITU_vs_Zombies.Menu.Screens
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using ITU_vs_Zombies.Game;
    using ITU_vs_Zombies.Graphics;
    using ITU_vs_Zombies.Scenery;
    using ITU_vs_Zombies.Stuff;

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;

    /// <summary>
    /// A screen where the player buy stuff.
    /// </summary>
    public class ShopScreen : GameScreen
    {
        #region Constants and Fields

        /// <summary>
        /// The cols.
        /// </summary>
        private const int Cols = 3;

        /// <summary>
        /// The rows.
        /// </summary>
        private const int Rows = 3;

        /// <summary>
        /// The shop.
        /// </summary>
        private readonly Shop shop;

        /// <summary>
        /// The background texture.
        /// </summary>
        private Texture2D backgroundTexture;

        /// <summary>
        /// The content.
        /// </summary>
        private ContentManager content;

        /// <summary>
        /// The matrix.
        /// </summary>
        private MatrixContainer matrix;

        /// <summary>
        /// The money font.
        /// </summary>
        private SpriteFont moneyFont;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ShopScreen"/> class.
        /// </summary>
        public ShopScreen()
        {
            this.shop = new Shop();
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The activate.
        /// </summary>
        /// <param name="instancePreserved">
        /// The instance preserved.
        /// </param>
        public override void Activate(bool instancePreserved)
        {
            if (instancePreserved)
            {
                return;
            }

            if (this.content == null)
            {
                this.content = new ContentManager(this.ScreenManager.Game.Services, "Content");
            }

            this.backgroundTexture = this.content.Load<Texture2D>("shopBackground");
            this.moneyFont = this.content.Load<SpriteFont>(@"fonts\Arial");
            this.shop.LoadItems();
            var list = new List<Tuple<string, Texture2D>>(this.shop.ShopList.Count);
            list.AddRange(
                from stuff in this.shop.ShopList
                let texture = this.content.Load<Texture2D>(@"Stuff\" + stuff.Image)
                select new Tuple<string, Texture2D>(stuff.Name + " " + stuff.Price, texture));

            this.matrix = new MatrixContainer(list, Rows, Cols);
            this.matrix.LoadContent(this.content);
            this.matrix.EnterPressed += this.EnterPressed;
        }

        /// <summary>
        /// The draw.
        /// </summary>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        public override void Draw(GameTime gameTime)
        {
            // SpriteBatch spriteBatch = this.ScreenManager.SpriteBatch;
            Viewport viewport = this.ScreenManager.GraphicsDevice.Viewport;
            var fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);
            this.ScreenManager.SpriteBatch.Begin();
            this.ScreenManager.SpriteBatch.Draw(this.backgroundTexture, fullscreen, Color.White);
            this.matrix.Draw(viewport.Width, viewport.Height, this.ScreenManager.SpriteBatch);
            this.DrawMoney(this.ScreenManager.SpriteBatch);
            this.ScreenManager.SpriteBatch.End();
        }

        /// <summary>
        /// The handle input.
        /// </summary>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        /// <param name="input">
        /// The input.
        /// </param>
        public override void HandleInput(GameTime gameTime, InputState input)
        {
            var escape = new InputAction(new[] { Keys.Escape }, true);

            if (escape.Evaluate(input))
            {
                Client.GameState = GameStateEnum.Normal;
                this.ExitScreen();
            }
            else
            {
                if (this.matrix != null)
                {
                    this.matrix.Move(input);
                }
            }
        }

        /// <summary>
        /// The unload.
        /// </summary>
        public override void Unload()
        {
            if (this.backgroundTexture != null)
            {
                this.backgroundTexture.Dispose();
            }

            if (this.matrix != null)
            {
                this.matrix.Dispose();
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The draw money.
        /// </summary>
        /// <param name="spriteBatch">
        /// The sprite batch.
        /// </param>
        private void DrawMoney(SpriteBatch spriteBatch)
        {
            // Draw money
            spriteBatch.DrawString(
                this.moneyFont, 
                "Money: " + Client.MainPlayer.Money, 
                new Vector2(10, 10),
                Color.OrangeRed, 
                0, 
                Vector2.Zero, 
                1, 
                SpriteEffects.None, 
                1);
        }

        /// <summary>
        /// The enter pressed.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        private void EnterPressed(string name)
        {
            string[] realName = name.Split(' ');
            ShopItem itemBought = this.shop.ShopList.Find(item => item.Name.Equals(realName[0]));
            if (itemBought.Price > Client.MainPlayer.Money)
            {
                return;
            }

            Client.MainPlayer.Money -= itemBought.Price;

            // If you have bought life
            if (realName[0].Equals("Life"))
            {
                var life = (Life)itemBought.Item;
                Client.MainPlayer.Life += (life.Value + Client.MainPlayer.Life) <= Client.MainPlayer.MaximumLife
                                              ? life.Value
                                              : (Client.MainPlayer.MaximumLife - Client.MainPlayer.Life);
            }
                
                // If you have bought a weapon
            else if (realName[0].Equals("Knife"))
            {
                var knife = (Knife)itemBought.Item.Clone();
                Client.MainPlayer.Weapons.Add(knife);
            }
            else if (realName[0].Equals("Gun"))
            {
                var firearm = (Firearm)itemBought.Item.Clone();
                Client.MainPlayer.Weapons.Add(firearm);
            }
        }

        #endregion
    }
}